package com.mdb.open.epay.service.epay;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.common.enums.ProductCodeEnum;
import com.mdb.open.epay.common.MdbDataSource;
import com.mdb.open.epay.config.EPayOpenProperties;
import com.mdb.open.epay.domain.po.epay.EpQuotaLimit;
import com.mdb.open.epay.enums.type.CcyTypeEnum;
import com.mdb.open.epay.enums.type.QuotaLimitTypeEnum;
import com.mdb.open.epay.enums.type.QuotaUserTypeEnum;
import com.mdb.open.epay.mapper.epay.EpQuotaLimitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 * 电子支付限额限次表 服务类
 *
 * @author liuyi
 * @date 2019-11-14
 */
@Service
@DS(MdbDataSource.EPAY)
public class EpQuotaLimitService extends SuperService<EpQuotaLimitMapper, EpQuotaLimit> {

    @Autowired
    private EPayOpenProperties ePayOpenProperties;

    /**
     * 獲取全行統一限額配置 默認MOP
     */
    public EpQuotaLimit getGlobalQuotaLimitConfig(QuotaLimitTypeEnum limitType, CcyTypeEnum ccyType) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<EpQuotaLimit>()
                .eq(EpQuotaLimit::getType, limitType.getValue())
                .eq(EpQuotaLimit::getCcy, ccyType.getValue())
                .eq(EpQuotaLimit::getProdCode, ProductCodeEnum.EP_QP.getValue())
                .eq(EpQuotaLimit::getQuotaType, QuotaUserTypeEnum.GLOBAL.getValue())
                .eq(EpQuotaLimit::getCustomerCode, ePayOpenProperties.getQuotaLimitBankDefaultVariable())
                .eq(EpQuotaLimit::getCustomerName, ePayOpenProperties.getQuotaLimitBankDefaultVariable())
                .eq(EpQuotaLimit::getCoreClientNo, ePayOpenProperties.getQuotaLimitBankDefaultVariable()));

    }

    /**
     * 獲取全行統一限次配置 默認MOP
     */
    public EpQuotaLimit getGlobalTimesLimitConfig(QuotaLimitTypeEnum limitType) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<EpQuotaLimit>()
                .eq(EpQuotaLimit::getType, limitType.getValue())
                .eq(EpQuotaLimit::getProdCode, ProductCodeEnum.EP_QP.getValue())
                .eq(EpQuotaLimit::getQuotaType, QuotaUserTypeEnum.GLOBAL.getValue())
                .eq(EpQuotaLimit::getCustomerCode, ePayOpenProperties.getQuotaLimitBankDefaultVariable())
                .eq(EpQuotaLimit::getCustomerName, ePayOpenProperties.getQuotaLimitBankDefaultVariable())
                .eq(EpQuotaLimit::getCoreClientNo, ePayOpenProperties.getQuotaLimitBankDefaultVariable()));

    }

    /**
     * 查詢用戶限額策略 默認MOP
     */
    public EpQuotaLimit getUserQpQuotaLimit(String coreClientNo, String coreAccNo, QuotaLimitTypeEnum limitType, CcyTypeEnum ccyType) {
        LambdaQueryWrapper<EpQuotaLimit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EpQuotaLimit::getType, limitType.getValue());
        wrapper.eq(EpQuotaLimit::getProdCode, ProductCodeEnum.EP_QP.getValue());
        wrapper.eq(EpQuotaLimit::getQuotaType, QuotaUserTypeEnum.USER.getValue());
        if (ObjectUtil.isNotEmpty(coreClientNo)) {
            wrapper.eq(EpQuotaLimit::getCoreClientNo, coreClientNo);
        }
        if (ObjectUtil.isNotEmpty(coreAccNo)) {
            wrapper.eq(EpQuotaLimit::getCoreAccNo, coreAccNo);
        }
        if (ObjectUtil.isNotEmpty(ccyType)) {
            wrapper.eq(EpQuotaLimit::getCcy, ccyType.getValue());
        }

        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 查詢用戶otp限额 默認MOP
     */
    public EpQuotaLimit getUserQpOtpLimit(String customerCode, CcyTypeEnum ccyTypeEnum) {
        LambdaQueryWrapper<EpQuotaLimit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EpQuotaLimit::getType, QuotaLimitTypeEnum.OTP.getValue());
        wrapper.eq(EpQuotaLimit::getProdCode, ProductCodeEnum.EP_QP.getValue());
        wrapper.eq(EpQuotaLimit::getCustomerCode, customerCode);
        wrapper.eq(EpQuotaLimit::getCcy, ccyTypeEnum.getValue());
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 查詢客戶單筆限額
     */
    public EpQuotaLimit getCustomerSigleQuotaLimit(String customerCode) {
        LambdaQueryWrapper<EpQuotaLimit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EpQuotaLimit::getType, QuotaLimitTypeEnum.QUOTA.getValue());
        wrapper.eq(EpQuotaLimit::getProdCode, ProductCodeEnum.EP_QP.getValue());
        wrapper.eq(EpQuotaLimit::getQuotaType, QuotaUserTypeEnum.CUSTOMER.getValue());
        wrapper.eq(EpQuotaLimit::getCustomerCode, customerCode);
        return this.baseMapper.selectOne(wrapper);
    }
}
